import { check } from 'meteor/check';
import { Notifications } from '/imports/models/Notifications';

/**
 * 获取未读通知
 * @param {Object} query
 * @param {Number} limit
 */
Meteor.publish('get.unread.notifications', function(query, limit) {
  check(query.user_id, String);
  check(limit, Number);

  let selector = {
    read: false,
    user_id: query.user_id
  };

  Counts.publish(this, 'get.unread.notifications.counts', Notifications.find(selector));

  return Notifications.find(selector, {
    limit: limit,
    sort: { createdAt: -1 }
  });
});

/**
 * 获取所有通知
 * @param {Object} query
 * @param {Number} limit
 */
Meteor.publish('get.notifications', function(query, limit) {
  check(query.user_id, String);
  check(limit, Number);

  let selector = {
    user_id: query.user_id
  };

  Counts.publish(this, 'get.notifications.counts', Notifications.find(selector));

  return Notifications.find(selector, {
    limit: limit,
    sort: { createdAt: -1 }
  });
});
